<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/application/forms/validators/Validator.php';
class ZipCodeValidator extends Validator
{
	private $regex = "/^\d{5}([\-]\d{4})?$/";
	private $shouldRequire;
	
	public function __construct(Form &$form, $key, $required=false, $message=null)
	{
		$this->isRequired    =  $required;
		$this->shouldRequire =  $required ? false : true;
		$this->form          =  $form;
		$this->key           =  $key;
		$this->message       =  $message;
	}
	
	public function validate()
	{	
		/*if(preg_match($this->regex, $this->input[$this->key]))
			$this->isValid = true;
		else
			$this->isValid = false;
			*/
			
		if($this->shouldRequire && strlen($this->form->{$this->key}) > 0)
			$this->isRequired = true;
		else
			$this->isRequred = $this->shouldRequire ? false : true;
		
			
		$this->isValid = filter_var($this->form->{$this->key}, 
									FILTER_VALIDATE_REGEXP, 
									array("options"=>array("regexp"=>$this->regex)));
	}
}
?>